package com.medishare.fragment;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.medishare.R;
import com.medishare.adapter.AdapterCallCenter;
import com.medishare.adapter.AdapterCommunityActivity;
import com.medishare.adapter.AdapterHealthTip;
import com.medishare.data.DataCallRecord;
import com.medishare.data.DataCommunityActivity;
import com.medishare.data.DataHeathTip;
import com.medishare.util.Utils;

import android.app.Activity;
import android.app.DialogFragment;
import android.app.Fragment;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

public class FragmentCommunityActivity extends Fragment{

	private ListView mListView;
	private Activity mainActivity;
	private TextView mPromptTV;
	private DialogFragment dialog;
    public final static String  TAG = FragmentCommunityActivity.class.getSimpleName();
    public final static boolean DEBUG = true;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_community_activity, null);
		mListView = (ListView) view.findViewById(R.id.list_health_tip);
		mPromptTV = (TextView) view.findViewById(R.id.prompt);
		String url = Utils.getBaseUrl() + "Event/read/?uuid=" + Utils.getDeviceUUID(mainActivity);
		if(DEBUG){
			Log.i(TAG,url);
		}
		loadData(url);
		return view;
	}

	private void loadData(String url){
		AsyncHttpClient client = new AsyncHttpClient();
		final ArrayList<DataCommunityActivity> datas = new ArrayList<DataCommunityActivity>(); 
		client.get(url, new AsyncHttpResponseHandler(){
			Message msg = new Message();
			@Override
			public void onSuccess(String content)  {
				super.onSuccess(content);
				if(DEBUG){
					Log.i(TAG,content);
				}
				try {
					JSONObject jsonObject = new JSONObject(content);
					int status = jsonObject.getInt("status");
					if(status == 0){
						msg.what = 1;
						mHandler.sendMessage(msg);
						return;
					}
					JSONArray jsonArray = jsonObject.getJSONArray("data");
					for(int i=0;i<jsonArray.length();i++){
						JSONObject tempObject = (JSONObject) jsonArray.get(i);
						DataCommunityActivity data =  new DataCommunityActivity();
						data.setName(tempObject.getString("name"));
						data.setUpdateTime(tempObject.getString("updateTime"));
						data.setConfirm(tempObject.getString("confirm"));
						data.setRemark(tempObject.getString("remark"));
						data.setAddress(tempObject.getString("address"));
						data.setDate(tempObject.getString("date"));
						datas.add(data);
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
				msg.what = 0;
				msg.obj = datas;
				mHandler.sendMessage(msg);
			}
			@Override
			public void onFailure(Throwable error, String content) {
				msg.what = -1;
				mHandler.sendMessage(msg);
				super.onFailure(error, content);
			}
		});
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		dialog = ProgressDialogFragment.newInstance();
		dialog.show(getFragmentManager().beginTransaction(), "progress");
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		mainActivity = activity;
	}
    
	Handler mHandler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			if (dialog != null && dialog.getDialog().isShowing()) {
				dialog.dismiss();
			}
			switch(msg.what){
			case 0:
				ArrayList<DataCommunityActivity> datas = (ArrayList<DataCommunityActivity>) msg.obj;
				AdapterCommunityActivity adapter = new AdapterCommunityActivity(mainActivity, datas);
				mListView.setAdapter(adapter);
				mListView.setVisibility(View.VISIBLE);
				mPromptTV.setVisibility(View.GONE);
				break;
			case 1:
				mListView.setVisibility(View.GONE);
				mPromptTV.setVisibility(View.VISIBLE);
				break;
			case -1:
				mListView.setVisibility(View.GONE);
				mPromptTV.setVisibility(View.VISIBLE);
				mPromptTV.setText(mainActivity.getResources().getString(
						R.string.server_error));
			}
			}
    };
	
	
}
